/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* ComGLHoodOView.java
*
* Created on November 15, 2007, 7:51 PM
 */
package gui.genomemap.view.outputView;

import genomemap.data.OrderElement;
import genomemap.worker.LHood;

import gui.genomemap.event.event.WorkerOutputEvent;
import gui.genomemap.outputModel.AnnealOModel;
import gui.genomemap.view.outputView.options.AnnealOOptions;

import javax.swing.JDialog;
import javax.swing.SwingUtilities;

/**
 *
 * @author  stewari1
 */
public class AnnealOView extends OutputView {

    /** Field description */
    private final AnnealOOptions optionsDialog = new AnnealOOptions(new javax.swing.JFrame(),
                                                     false);


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.JTextField convgOrder_JTextField;

    /** Field description */
    private javax.swing.JLabel jLabel1;

    /** Field description */
    private javax.swing.JLabel jLabel2;

    /** Field description */
    private javax.swing.JLabel lhood_JLabel;

    /** Field description */
    private javax.swing.JButton options_jButton;


    // End of variables declaration//GEN-END:variables

    /** Creates new form ComGLHoodOView */
    public AnnealOView() {

        initComponents();
        optionsDialog.setOutputView(this);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1               = new javax.swing.JLabel();
        options_jButton       = new javax.swing.JButton();
        jLabel2               = new javax.swing.JLabel();
        convgOrder_JTextField = new javax.swing.JTextField();
        lhood_JLabel          = new javax.swing.JLabel();

        jLabel1.setText("Simulated Annealing Stopped with Likelihood :  ");
        options_jButton.setText("Options");
        options_jButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                options_jButtonActionPerformed(evt);
            }

        });
        jLabel2.setText("The Best Order found is:");
        lhood_JLabel.setText("---");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

        this.setLayout(layout);
        layout.setHorizontalGroup(layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup().addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lhood_JLabel))
                            .addGroup(layout.createSequentialGroup().addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
                                    .UNRELATED)
                                        .addComponent(convgOrder_JTextField,
                                            javax.swing.GroupLayout.PREFERRED_SIZE, 226,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                    .addComponent(options_jButton))
                                                        .addContainerGap(284, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1).addComponent(lhood_JLabel)).addGap(9, 9, 9)
                        .addGroup(layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(convgOrder_JTextField,
                                          javax.swing.GroupLayout.PREFERRED_SIZE,
                                          javax.swing.GroupLayout.DEFAULT_SIZE,
                                          javax.swing.GroupLayout.PREFERRED_SIZE))
                                              .addPreferredGap(javax.swing.LayoutStyle
                                                  .ComponentPlacement.UNRELATED)
                                                      .addComponent(options_jButton)
                                                          .addGap(26, 26, 26)));

    }    // </editor-fold>//GEN-END:initComponents

    /**
     * Method description
     *
     * @param evt description
     */
    private void options_jButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_options_jButtonActionPerformed

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                optionsDialog.setLocationRelativeTo(null);
                optionsDialog.setVisible(true);
            }

        });

    }    // GEN-LAST:event_options_jButtonActionPerformed

    @Override
    public void refresh() {

        if (getOutputModel() == null) {

            options_jButton.setEnabled(false);

            return;
        }

        AnnealOModel annealOModel = (AnnealOModel) getOutputModel();

        lhood_JLabel.setText("" + annealOModel.getConvValue());
        convgOrder_JTextField.setText(OrderElement.createOrderString(annealOModel.getOrderSet()));
        optionsDialog.setOptions(annealOModel);
        options_jButton.setEnabled(true);
    }

    @Override
    public JDialog getOptionsDialog() {
        return optionsDialog;
    }

    @Override
    public void doAnnealOutput(WorkerOutputEvent e) {


        // the view(OutputView) is listening from the working process now
        // launch runnables on swing thread according to the runtime display options set
        // in your output model which is the same instance the process worker has.
        // ontain information on the worker using e.getSource().. this case it will be an LHood object
        LHood lhood = (LHood) e.getSource();


        // update according to the options set
        Runnable runnable = new Runnable() {
            public void run() {}
        };

        SwingUtilities.invokeLater(runnable);
    }
}
